package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.R;
import com.example.entity.*;
import com.example.service.DishService;
import com.example.service.OrdersListService;
import com.example.service.ShopCarService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("ordersLists")
public class OrdersListController {


    @Autowired
    private OrdersListService ordersListService;
    @Autowired
    private ShopCarService shopCarService;

    @Autowired
    private DishService dishService;
    

    /**
     * 分页查询所有订单信息
     * @return
     */

    @GetMapping("/getPageOrdersList/{currentPage}/{pageSize}")

    public R getPageOrdersList(@PathVariable int currentPage,@PathVariable int pageSize,OrdersList ordersList){

        IPage page=new Page(currentPage,pageSize);

        LambdaQueryWrapper<OrdersList> lqw =new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(ordersList.getOrderNumber()),OrdersList::getOrderNumber,ordersList.getOrderNumber());

        IPage page1 = ordersListService.page(page,lqw);

        return R.success("分页查询所有ordersList成功！",page1);
    }

    /**
     * 分页查询个人订单信息
     * @return
     */
    @GetMapping("/getPageOrdersListById/{currentPage}/{pageSize}")

    public R getPageOrdersListById(@PathVariable int currentPage,@PathVariable int pageSize,OrdersList ordersList){

        IPage page=new Page(currentPage,pageSize);

        LambdaQueryWrapper<OrdersList> lqw =new LambdaQueryWrapper<>();
        lqw.like(ordersList.getUserId()!=null,OrdersList::getUserId,ordersList.getUserId());

        IPage page1 = ordersListService.page(page,lqw);

        return R.success("分页查询个人订单信息成功！",page1);
    }


    /**
     * 添加当前用户所有购物车的东西
     * @param ordersList
     * @return
     */
    @PostMapping("/addlist")
    public R add(@RequestBody OrdersList ordersList) {
        List<OrdersList> ordersListList =new ArrayList<>();

        LambdaQueryWrapper<ShopCar> lqw =new LambdaQueryWrapper<>();
        lqw.eq(ShopCar::getUserId,ordersList.getUserId());
        List<ShopCar> list = shopCarService.list(lqw);

        OrdersList ordersList1=new OrdersList();

        for (int i = 0; i < list.size(); i++) {

            // 下单时间
            ordersList1.setTime(ordersList.getTime());
            // 订单号
            ordersList1.setOrderNumber(ordersList.getOrderNumber());


            ordersList1.setUserId(ordersList.getUserId());

            ordersList1.setDishId(list.get(i).getDishId());
            ordersList1.setImage(list.get(i).getImage());
            ordersList1.setRemarks(list.get(i).getRemarks());
            ordersList1.setPrice(list.get(i).getPrice());
            ordersList1.setTotal(list.get(i).getTotal());
            ordersList1.setNumber(list.get(i).getNumber());

            ordersListList.add(ordersList1);
            System.out.println(ordersList1);
            ordersListService.save(ordersList1);
        }


//        Boolean adds = ordersListService.saveBatch(ordersListList);
//
//        if (adds){
//            return R.success("添加ordersList成功!",ordersList);
//        }
//        return R.error("添加ordersList失败!");
        return R.success("添加ordersList成功!",ordersList);
    }


    /**
     * 修改
     * @param ordersList
     * @return
     */
    @PutMapping("/updateOrder")
    public R deleteById(@RequestBody OrdersList ordersList){

        Boolean update = ordersListService.updateById(ordersList);
        if (update){
            return R.success("修改ordersList成功!",null);
        }
        return R.error("修改ordersList失败!");
    }

    /**
     * 菜品销量排名查询
     *
     * @return
     */
    @GetMapping("/getByBuyNumber")
    public R getByBuyNumber(){
        List<DishData> list=ordersListService.getByBuyNumber();

        for (int i = 0; i < list.size(); i++) {
            //通过dish_id查询菜名 然后封装
            LambdaQueryWrapper<Dish> lqw =new LambdaQueryWrapper<>();
            lqw.eq(Dish::getId,list.get(i).getDishId());
            Dish one = dishService.getOne(lqw);

            //封装菜名
            list.get(i).setName(one.getName());
        }

        return R.success("菜品销量排名查询成功",list);

    }




}

